package com.baizhi.service.impl;

import com.baizhi.dao.StudentDAO;
import com.baizhi.dto.StudentDTO;
import com.baizhi.entity.Student;
import com.baizhi.entity.StudentTag;
import com.baizhi.feign.FeignTag;
import com.baizhi.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.DateUtil;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentDAO, Student> implements StudentService {

    @Autowired
    private StudentDAO studentDAO;
    @Autowired
    private FeignTag feignTag;

    @Override
    public List<Student> queryAll() {
        List<Student> students = studentDAO.selectList(null);
        return students;
    }

    /**
     * 添加学生
     *
     * @param studentDTO
     */
    @Override
    public Student add(StudentDTO studentDTO) {
        Student entity = new Student();
        entity.setName(studentDTO.getName());
        Date bir = studentDTO.getBir();
        //根据生日算出年龄
        int age = DateUtil.getAge(bir);
        entity.setAge(age);
        entity.setBir(bir);
        entity.setQq(studentDTO.getQq());
        entity.setPhone(studentDTO.getPhone());
        //星座
        String constellation = DateUtil.getConstellation(bir);
        entity.setStarts(constellation);
        entity.setAttr(DateUtil.getYear(bir));
        entity.setMark(studentDTO.getMark());
        entity.setClazzid(studentDTO.getClazzId());
        entity.setGroupid(studentDTO.getGroupId());
        entity.setCityid(studentDTO.getCityId());
        int insert = studentDAO.insert(entity);
        //添加学生标签
        /**
         * 遍历标签，将学生id传过去
         */
        Long id = entity.getId();
        System.out.println("id = " + id);
        Student student = studentDAO.selectById(id);

        List<Long> tagIds = studentDTO.getTagIds();
        for (Long tagId : tagIds) {
            StudentTag studentTag = new StudentTag(student.getId(), tagId);
            feignTag.addTags(studentTag);
        }
        return student;

    }
}
